/*
 * Copyright (C) 2012  E-dance Studio Harbin Institute of Technology 
 * Project:campusmobile
 * Author: 初楷博
 * Create Date: 2012-5-31下午08:30:54
 */
package hit.edance.campusmobile.chat.adapter;

import java.util.List;

import hit.edance.campusmobile.chat.R;
import hit.edance.campusmobile.chat.model.ChatMessage;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**   
 * 聊天界面的adapter
 * 
 * @Project campusmobile
 * @Package hit.edance.campusmobile.chat.adapter
 * @Class ChattingAdapter
 * @Date 2012-5-31 下午08:30:54
 * @author 初楷博
 * @version 1.0
 * @since 1.0
 */
public class ChattingAdapter extends BaseAdapter{
	
	private Context context;
	private List<ChatMessage> messages;
	
	public ChattingAdapter(Context context,List<ChatMessage> messages){
		super();
		this.messages = messages;
		this.context = context;
		
	}

	public int getCount() {
		return messages.size();
	}

	public ChatMessage getItem(int position) {
		return messages.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		ChatMessage message = messages.get(position);
		if (convertView == null
				|| (holder = (ViewHolder) convertView.getTag()).flag != message
						.getDirection()) {
			holder = new ViewHolder();
			if (message.getDirection() == ChatMessage.MESSAGE_FROM) {
				holder.flag = ChatMessage.MESSAGE_FROM;

				convertView = LayoutInflater.from(context).inflate(
						R.layout.chat_chatting_item_from, null);
			} else {
				holder.flag = ChatMessage.MESSAGE_TO;
				convertView = LayoutInflater.from(context).inflate(
						R.layout.chat_chatting_item_to, null);
			}

			holder.text = (TextView) convertView
					.findViewById(R.id.chatting_content_itv);
			convertView.setTag(holder);
		}
		holder.text.setText(message.getContent());

		return convertView;
	}

	class ViewHolder {
		TextView text;
		int flag;
	}

}
